package com.zhuojy.equity.mall.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.DocumentClass;
import com.zhuojy.equity.mall.admin.common.entity.vo.BalanceExportEntity;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.dal.mapper.DocumentClassMapper;
import com.zhuojy.equity.mall.admin.manager.DocumentClassManager;
import com.zhuojy.equity.mall.admin.manager.DocumentManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.nimbus.State;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DocumentClassService extends ServiceImpl <DocumentClassMapper,DocumentClass> {
    private  final DocumentClassManager documentClassManager;

    public IPage queryDocumentClass(DocumentClass documentClass) {
        IPage page = new Page<>(documentClass.getCurrentPage(),documentClass.getPageSize());
        LambdaQueryWrapper<DocumentClass> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasText(documentClass.getName())){
            lambdaQueryWrapper.like(DocumentClass::getName,documentClass.getName());
        }
        if (documentClass.getState()!=null){
            lambdaQueryWrapper.like(DocumentClass::getState,documentClass.getState());
        }
        page(page,lambdaQueryWrapper);
        return page;
    }

    /**
     * 删除
     * @param id
     */
    public void deleteDocumentClass(Integer id) {
        this.baseMapper.deleteById(id);
    }

    /**
     * 新增类型
     * @param documentClass
     */
    public void addDocumentClass(DocumentClass documentClass) {
        if(documentClass.getId()!=null){

            this.baseMapper.updateById(documentClass);
        }else {
            documentClass.setState(2);
            this.baseMapper.insert(documentClass);
        }
    }

    /**
     * 回显
     * @param id
     * @return
     */
    public DocumentClass selectDocumentClass(Integer id) {
        DocumentClass documentClass = this.baseMapper.selectById(id);
        return documentClass;
    }
    /**
     * 修改
     * @param documentClass
     */
    public void updateDocumentClass(DocumentClass documentClass) {
        this.baseMapper.updateById(documentClass);
    }

    /**
     *  停用，停用
     * @param id
     *
     */
    public void updateStateDocumentClass(Long id,Integer state) {
        DocumentClass documentClass = baseMapper.selectById(id);
        documentClass.setState(state); // 状态
        this.baseMapper.updateById(documentClass);


    }

    //导出
    public void exportExcel(HttpServletResponse response) {
            // 从数据库中获取数据列表
            List<DocumentClass> list = baseMapper.selectList(null);
            // 调用导出方法
            ExcelUtils.export(response, "导出数据列表", list, DocumentClass.class);
    }
}

